package com.itany.shop.servlet;

import com.itany.shop.entity.User;
import com.itany.shop.exception.CodeErrorException;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserExistException;
import com.itany.shop.exception.UsernameNotExistException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.UserService;
import com.itany.shop.vo.Cart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 黄衡熙
 * @version 1.0
 */
@WebServlet("*.user")
public class UserServlet extends HttpServlet {
protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException{
    req.setCharacterEncoding("utf-8");
    resp.setContentType("text/html;charset=utf-8");
    String path=req.getServletPath();
    switch(path){
        case "/register.user":
            register(req,resp);
            break;
        case "/login.user":
            login(req,resp);
            break;
        case "/logout.user":
            logout(req,resp);
            break;
            
        default:
            PrintWriter out = resp.getWriter();
            out.println("<h1 style='color:red;'>对不起,您访问的资源有误,请检查请求是否正确</h1>");
    }
}
    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService userService = (UserService) ObjectFactory.getObject("userService");
        // 获取请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");

        try {
            // 调用业务层方法实现核心业务
            userService.register(username,password,phone,address);
            resp.sendRedirect(req.getContextPath()+"/jsp/login.jsp");
        } catch (RequestParameterErrorException e) {
            req.setAttribute("registerMsg",e.getMessage());
            req.getRequestDispatcher("/jsp/register.jsp").forward(req,resp);
        } catch (UserExistException e) {
            req.setAttribute("registerMsg",e.getMessage());
            req.getRequestDispatcher("/jsp/register.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("registerMsg","服务器内部异常");
            req.getRequestDispatcher("/jsp/register.jsp").forward(req,resp);
        }
    }

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService userService = (UserService) ObjectFactory.getObject("userService");
        // 获取请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("code");
        // 获取图片中的验证码内容
        String image = (String) req.getSession().getAttribute("code");


        try {
            User user = userService.login(username, password, code, image);
            // 登录成功之后,将用户信息保存到session中
            req.getSession().setAttribute("user",user);
            // 用户登录进来之后,分配一个新的购物车,保存到session中
            req.getSession().setAttribute("cart",new Cart());
            // 重定向到主页
            resp.sendRedirect(req.getContextPath()+"/findAll.product");
        } catch (RequestParameterErrorException e) {
            req.setAttribute("loginMsg",e.getMessage());
            req.getRequestDispatcher("/jsp/login.jsp").forward(req,resp);
        } catch (CodeErrorException e) {
            req.setAttribute("loginMsg",e.getMessage());
            req.getRequestDispatcher("/jsp/login.jsp").forward(req,resp);
        } catch (UsernameNotExistException e) {
            req.setAttribute("loginMsg",e.getMessage());
            req.getRequestDispatcher("/jsp/login.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("loginMsg", "服务器内部异常");
            req.getRequestDispatcher("/jsp/login.jsp").forward(req, resp);
        }

    }

    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().invalidate();
        resp.sendRedirect(req.getContextPath()+"/findAll.product");
    }
}
